package com.sim.serialport.library.seria.ext

import android.annotation.SuppressLint

fun ByteArray.bytesToHex(): String {
    val sb = StringBuilder()
    for (b in this) {
        sb.append(String.format("%02X ", b))
    }
    return sb.toString().trim { it <= ' ' }
}

// Extension function to convert ByteArray to Hex String
fun ByteArray.bytesToHex2(): String {
    return joinToString("") { "%02x".format(it) }
}

fun String.hexStringToByteArray(): ByteArray {
    val len = this.length
    val data = ByteArray(len / 2)
    for (i in 0 until len step 2) {
        data[i / 2] =
            ((Character.digit(this[i], 16) shl 4) + Character.digit(this[i + 1], 16)).toByte()
    }
    return data
}

fun String.hexStringToAscii(): String {
    val output = StringBuilder("")

    // 每两个字符一组，转换为字节
    try {
        for (i in this.indices step 2) {
            val str = this.substring(i, i + 2)
            val decimal = str.toInt(16) // 将十六进制字符串转换为十进制整数
            output.append(decimal.toChar()) // 将十进制整数转换为字符并添加到输出
        }
    } catch (e: Exception) {
        e.stackTraceToString()
    }

    return output.toString()
}

@SuppressLint("DefaultLocale")
fun generate12DigitTimestamp(): String {
    val timestamp = System.currentTimeMillis()
    val uniquePart = (timestamp % 1_000_000_000) // 取时间戳的后9位
    val randomPart = (Math.random() * 1000).toInt() // 生成3位随机数

    return String.format("%09d%03d", uniquePart, randomPart)
}

fun asciiToHex(asciiStr: String): String {
    val hexString = StringBuilder()
    for (char in asciiStr) {
        hexString.append(String.format("%02X", char.code))
    }
    return hexString.toString()
}

fun String.toHex(): String {
    val hexString = StringBuilder()
    for (char in this) {
        hexString.append(String.format("%02X", char.code))
    }
    return hexString.toString()
}

fun asciiToHex(asciiStr: String, length: Int): String {
    // 将字符串长度调整为指定的长度
    val hexResult = asciiStr.padStart(length, '0')
    if (hexResult.length > length) hexResult.takeLast(length) else hexResult
    val hexString = StringBuilder()
    for (char in hexResult) {
        hexString.append(String.format("%02X", char.code))
    }
    return hexString.toString()
}

fun String.amountToHex(): String {
    // 将字符串长度调整为指定的长度
    val hexResult = this.padStart(12, '0')
    if (hexResult.length > 12) hexResult.takeLast(12) else hexResult
    val hexString = StringBuilder()
    for (char in hexResult) {
        hexString.append(String.format("%02X", char.code))
    }
    return hexString.toString()
}


// 字符串处理， ****** -> ** ** **
fun formatString(input: String): String {
    require(input.length % 2 == 0) { "输入的字符串长度必须是偶数" }

    val result = StringBuilder()

    for (i in input.indices step 2) {
        if (i > 0) {
            result.append(" ")
        }
        result.append(input.substring(i, i + 2))
    }

    return result.toString()
}

fun Int.toFourLength(): String {
    val padStart = this.toString().padStart(4, '0')
    return padStart
}

